﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Document_Management
{
    public partial class frmVehicleMnt : Form
    {
        int width, height;
        int iflag = 0;
        bool b車輛Search, b車輛Insert, b車輛Edit, b車輛Del;
        string car;

        DateTime date;
        static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        public frmVehicleMnt(bool b車輛Search, bool b車輛Insert, bool b車輛Edit, bool b車輛Del)
        {
            InitializeComponent();
            width = this.Size.Width;
            height = this.Size.Height;
            this.b車輛Search = b車輛Search;
            this.b車輛Insert = b車輛Insert;
            this.b車輛Edit = b車輛Edit;
            this.b車輛Del = b車輛Del;
        }

        private void frmVehicleMnt_Load(object sender, EventArgs e)
        {
            Load_車輛();
            if (!b車輛Insert)
            {
                車輛_新增.Enabled = false;
                車輛人員資料_新增.Enabled = false;
                bt_中心新增.Enabled = false;
                車輛廠牌_新增.Enabled = false;
                車種_新增.Enabled = false;
                車輛型號訊息_新增.Enabled = false;
            }
            if(!b車輛Edit){
                車輛型號訊息_修改.Enabled = false;
                車種_修改.Enabled = false;
                車輛廠牌_修改.Enabled = false;
                bt_中心修改.Enabled = false;
                車輛人員資料_修改.Enabled = false;
                車輛_修改.Enabled = false;
            }
            if(!b車輛Del){
                車輛_刪除.Enabled = false;
                車輛人員資料_查詢.Enabled = false;
                bt_中心查詢.Enabled = false;
                車輛廠牌_刪除.Enabled = false;
                車種_刪除.Enabled = false;
                車輛型號訊息_刪除.Enabled = false;
            }
            if (!b車輛Search)
            {
                車輛_查詢.Enabled = false;
            }
        }

        public void Load_車輛成本中心()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_load_車輛成本中心", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridCenter.DataSource = dt;
                PublicClass.setRowNumber(dataGridCenter);
                foreach (DataGridViewRow row in dataGridCenter.Rows)
                {
                    if (row.Index == dataGridCenter.Rows.Count - 1) break;
                    row.HeaderCell.Value = (row.Index + 1).ToString();
                    row.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                }
                dataGridCenter.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                con.Close();
                txt_成本中心代號.Text = "";
                txt_名稱.Text = "";
                txt_備註.Text = "";
                gBAdd_車輛.Visible = false;
                this.Size = new System.Drawing.Size(this.Size.Width, height - gBAdd_車輛.Height);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void bt_中心新增_Click(object sender, EventArgs e)
        {
            
           try{
                    if(txt_成本中心代號.Text.ToString()==null||txt_成本中心代號.Text.ToString().Equals(""))
                    MessageBox.Show("成本中心代號還沒輸入資料");
                    else
                    {
                        SqlConnection con = new SqlConnection(connectionString);
                        con.Open();
                        SqlCommand cmd0;
                        String sql = "SELECT COUNT(*) FROM 車輛成本中心 WHERE 成本中心代號='" + txt_成本中心代號.Text.ToString() + "'";
                        cmd0 = new SqlCommand(sql, con);
                        if ((int)cmd0.ExecuteScalar() > 0)
                        {
                            MessageBox.Show("Error! '" + txt_成本中心代號.Text.ToString()+"' 由來");
                        }
                        else
                        {
                            cmd0 = new SqlCommand("sp_車輛成本中心_新增", con);
                            cmd0.CommandType = CommandType.StoredProcedure;
                            cmd0.Parameters.AddWithValue("@成本中心代號", txt_成本中心代號.Text.ToString());
                            cmd0.Parameters.AddWithValue("@名稱", txt_名稱.Text.ToString());
                            cmd0.Parameters.AddWithValue("@備註", txt_備註.Text.ToString());
                            cmd0.ExecuteNonQuery();
                            MessageBox.Show("新增成功");
                            Load_車輛成本中心();
                        }
                        con.Close();
                    }
                }
                catch (Exception ex) 
                {
                    MessageBox.Show(ex.ToString());
                    return;
                }
        }

        private void bt_中心修改_Click(object sender, EventArgs e)
        {
            try
            {
                if(txt_成本中心代號.Text.ToString()==null||txt_成本中心代號.Text.ToString().Equals(""))
                    MessageBox.Show("成本中心代號還沒輸入資料");
                else
                {
                    DialogResult answer = MessageBox.Show("您確定要修改資料 ?", "警報", MessageBoxButtons.YesNo);
                    if (answer == DialogResult.Yes)
                    {
                        SqlConnection con = new SqlConnection(connectionString);
                        con.Open();
                        SqlCommand cmd0 = new SqlCommand("sp_車輛成本中心_修改", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@成本中心代號1", car);
                        cmd0.Parameters.AddWithValue("@成本中心代號2", txt_成本中心代號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@名稱", txt_名稱.Text.ToString());
                        cmd0.Parameters.AddWithValue("@備註", txt_備註.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("修改成功");
                        con.Close();
                    }
                    Load_車輛成本中心();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void bt_中心查詢_Click(object sender, EventArgs e)
        {
             try
            {
                 if(txt_成本中心代號.Text.ToString()==null||txt_成本中心代號.Text.ToString().Equals(""))
                    MessageBox.Show("成本中心代號還沒輸入資料");
                 else
                 {
                     DialogResult answer = MessageBox.Show("您確定要刪除資料 ?", "警報", MessageBoxButtons.YesNo);
                     if (answer == DialogResult.Yes)
                     {
                         SqlConnection con = new SqlConnection(connectionString);
                         con.Open();
                         SqlCommand cmd0 = new SqlCommand("sp_車輛成本中心_刪除", con);
                         cmd0.CommandType = CommandType.StoredProcedure;
                         cmd0.Parameters.AddWithValue("@成本中心代號", txt_成本中心代號.Text.ToString());
                         cmd0.ExecuteNonQuery();
                         MessageBox.Show("刪除成功");
                         con.Close();
                     }
                     Load_車輛成本中心();
                 }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        /**
         * 車輛人員資料
         **/

        public void Load_車輛人員資料()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_車輛人員資料_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGrid_車輛人員資料.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dataGrid_車輛人員資料.DataSource = dt;
                PublicClass.setRowNumber(dataGrid_車輛人員資料);
                con.Close();
                txt_人員代號.Text = "";
                txt_人員名稱.Text = "";
                txt_部門代號.Text = "";
                txt_部門名稱.Text = "";
                txt_成本中心.Text = "";
                txt_電話.Text = "";
                gBAdd_車輛.Visible = false;
                this.Size = new System.Drawing.Size(this.Size.Width, height - gBAdd_車輛.Height);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        //load 車輛廠牌
        public void Load_車輛廠牌()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_車輛廠牌_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGrid_車輛廠牌.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dataGrid_車輛廠牌.DataSource = dt;
                PublicClass.setRowNumber(dataGrid_車輛廠牌);
                con.Close();
                txt_車輛廠牌_備註.Text = "";
                txt_車輛廠牌_名稱.Text = "";
                txt_廠牌代號.Text = "";
                gBAdd_車輛.Visible = false;
                this.Size = new System.Drawing.Size(this.Size.Width, height - gBAdd_車輛.Height);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        //load 車種
        public void Load_車種()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_車種_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGrid_車種.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dataGrid_車種.DataSource = dt;
                PublicClass.setRowNumber(dataGrid_車種);
                con.Close();
                txt_車種_車種代號.Text = "";
                txt_車種_名稱.Text = "";
                txt_車種_備註.Text = "";
                gBAdd_車輛.Visible = false;
                this.Size = new System.Drawing.Size(this.Size.Width, height - gBAdd_車輛.Height);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        //load 車輛型號訊息
        public void Load_車輛型號訊息()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_車輛型號訊息_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGrid_車輛型號訊息.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dataGrid_車輛型號訊息.DataSource = dt;
                PublicClass.setRowNumber(dataGrid_車輛型號訊息);
                con.Close();
                txt_車輛型號訊息_備註.Text = "";
                txt_車輛型號訊息_名稱.Text = "";
                txt_車輛型號訊息_型號.Text = "";
                gBAdd_車輛.Visible = false;
                this.Size = new System.Drawing.Size(this.Size.Width, height - gBAdd_車輛.Height);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void 車輛人員資料_新增_Click(object sender, EventArgs e)
        {
            try
            {
                if(txt_人員代號.Text.ToString()==null||txt_人員代號.Text.ToString().Equals(""))
                    MessageBox.Show("人員代號還沒輸入資料");
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    String sql = "SELECT count(*) FROM 車輛人員資料 WHERE 人員代號='" + txt_人員代號.Text.ToString() + "'";
                    SqlCommand cmd0;
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() > 0)
                    {
                        MessageBox.Show("Error! '"+txt_人員代號.Text.ToString()+"' 由來");
                    }
                    else
                    {
                        cmd0 = new SqlCommand("sp_車輛人員資料_新增", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@人員代號", txt_人員代號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@人員名稱", txt_人員名稱.Text.ToString());
                        cmd0.Parameters.AddWithValue("@部門代號", txt_部門代號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@部門名稱", txt_部門名稱.Text.ToString());
                        cmd0.Parameters.AddWithValue("@成本中心代號", txt_成本中心.Text.ToString());
                        cmd0.Parameters.AddWithValue("@電話", txt_電話.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("新增成功");
                        Load_車輛人員資料();
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void 車輛人員資料_修改_Click(object sender, EventArgs e)
        {
            try
            {
                if(txt_人員代號.Text.ToString()==null||txt_人員代號.Text.ToString().Equals(""))
                    MessageBox.Show("人員代號還沒輸入資料");
                else
                {
                    DialogResult answer = MessageBox.Show("您確定要刪除資料 ?", "警報", MessageBoxButtons.YesNo);
                    if (answer == DialogResult.Yes)
                    {
                        SqlConnection con = new SqlConnection(connectionString);
                        con.Open();
                        SqlCommand cmd0 = new SqlCommand("sp_車輛人員資料_修改", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@人員代號1", car);
                        cmd0.Parameters.AddWithValue("@人員代號2", txt_人員代號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@人員名稱", txt_人員名稱.Text.ToString());
                        cmd0.Parameters.AddWithValue("@部門代號", txt_部門代號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@部門名稱", txt_部門名稱.Text.ToString());
                        cmd0.Parameters.AddWithValue("@成本中心代號", txt_成本中心.Text.ToString());
                        cmd0.Parameters.AddWithValue("@電話", txt_電話.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("修改成功");
                        con.Close();
                    }
                    Load_車輛人員資料();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void 車輛人員資料_查詢_Click(object sender, EventArgs e)
        {
            try
            {
                if(txt_人員代號.Text.ToString()==null||txt_人員代號.Text.ToString().Equals(""))
                    MessageBox.Show("人員代號還沒輸入資料");
                else
                {
                    DialogResult answer = MessageBox.Show("您確定要刪除資料 ?", "警報", MessageBoxButtons.YesNo);
                    if (answer == DialogResult.Yes)
                    {
                        SqlConnection con = new SqlConnection(connectionString);
                        con.Open();
                        SqlCommand cmd0 = new SqlCommand("sp_車輛人員資料_刪除", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@人員代號", txt_人員代號.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("刪除成功");
                        con.Close();
                    }
                    Load_車輛人員資料();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if(txt_廠牌代號.Text.ToString()==null||txt_廠牌代號.Text.ToString().Equals(""))
                    MessageBox.Show("廠牌代號還沒輸入資料");
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    String sql = "SELECT COUNT(*) FROM 車輛廠牌 WHERE 廠牌代號='" + txt_廠牌代號.Text.ToString() + "'";
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() > 0)
                    {
                        MessageBox.Show("Error! '" + txt_廠牌代號.Text.ToString() + "' 由來");
                    }
                    else
                    {
                        cmd0 = new SqlCommand("sp_車輛廠牌_新增", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@廠牌代號", txt_廠牌代號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@名稱", txt_車輛廠牌_名稱.Text.ToString());
                        cmd0.Parameters.AddWithValue("@備註", txt_車輛廠牌_備註.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("新增成功");
                        Load_車輛廠牌();
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void 車輛廠牌_修改_Click(object sender, EventArgs e)
        {
            try
            {
                if(txt_廠牌代號.Text.ToString()==null||txt_廠牌代號.Text.ToString().Equals(""))
                    MessageBox.Show("廠牌代號還沒輸入資料");
                else
                {
                    DialogResult answer = MessageBox.Show("您確定要修改資料 ?", "警報", MessageBoxButtons.YesNo);
                    if (answer == DialogResult.Yes)
                    {
                        SqlConnection con = new SqlConnection(connectionString);
                        con.Open();
                        SqlCommand cmd0 = new SqlCommand("sp_車輛廠牌_修改", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@廠牌代號1", car);
                        cmd0.Parameters.AddWithValue("@廠牌代號2", txt_廠牌代號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@名稱", txt_車輛廠牌_名稱.Text.ToString());
                        cmd0.Parameters.AddWithValue("@備註", txt_車輛廠牌_備註.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("修改成功");
                        con.Close();
                    }
                    Load_車輛廠牌();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void 車輛廠牌_刪除_Click(object sender, EventArgs e)
        {
            try
            {
                if(txt_廠牌代號.Text.ToString()==null||txt_廠牌代號.Text.ToString().Equals(""))
                    MessageBox.Show("廠牌代號還沒輸入資料");
                else
                {
                    DialogResult answer = MessageBox.Show("您確定要刪除資料 ?", "警報", MessageBoxButtons.YesNo);
                    if (answer == DialogResult.Yes)
                    {
                        SqlConnection con = new SqlConnection(connectionString);
                        con.Open();
                        SqlCommand cmd0 = new SqlCommand("sp_車輛廠牌_刪除", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@廠牌代號", txt_廠牌代號.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("刪除成功");
                        con.Close();
                    }
                    Load_車輛廠牌();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void dataGrid_車輛廠牌_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            
        }

        //車種管理

        private void 車種_新增_Click(object sender, EventArgs e)
        {
            try
            {
                if(txt_車種_車種代號.Text.ToString()==null||txt_車種_車種代號.Text.ToString().Equals(""))
                    MessageBox.Show("車種代號還沒輸入資料");
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    String sql = "SELECT COUNT(*) FROM 車種 WHERE 車種代號='" + txt_車種_車種代號.Text.ToString() + "'";
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() > 0)
                    {
                        MessageBox.Show("Error! '" + txt_車種_車種代號.Text.ToString() + "' 由來");
                    }
                    else
                    {
                        cmd0 = new SqlCommand("sp_車種_新增", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@車種代號", txt_車種_車種代號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@名稱", txt_車種_名稱.Text.ToString());
                        cmd0.Parameters.AddWithValue("@備註", txt_車種_備註.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("新增成功");
                        Load_車種();
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void 車種_修改_Click(object sender, EventArgs e)
        {
            try
            {
                if(txt_車種_車種代號.Text.ToString()==null||txt_車種_車種代號.Text.ToString().Equals(""))
                    MessageBox.Show("車種代號還沒輸入資料");
                else
                {
                    DialogResult answer = MessageBox.Show("您確定要修改資料 ?", "警報", MessageBoxButtons.YesNo);
                    if (answer == DialogResult.Yes)
                    {
                        SqlConnection con = new SqlConnection(connectionString);
                        con.Open();
                        SqlCommand cmd0 = new SqlCommand("sp_車種_修改", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@車種代號1", car);
                        cmd0.Parameters.AddWithValue("@車種代號2", txt_車種_車種代號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@名稱", txt_車種_名稱.Text.ToString());
                        cmd0.Parameters.AddWithValue("@備註", txt_車種_備註.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("修改成功");
                        con.Close();
                    }
                    Load_車種();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void 車種_刪除_Click(object sender, EventArgs e)
        {
            try
            {
                if(txt_車種_車種代號.Text.ToString()==null||txt_車種_車種代號.Text.ToString().Equals(""))
                    MessageBox.Show("車種代號還沒輸入資料");
                else
                {
                    DialogResult answer = MessageBox.Show("您確定要刪除資料 ?", "警報", MessageBoxButtons.YesNo);
                    if (answer == DialogResult.Yes)
                    {
                        SqlConnection con = new SqlConnection(connectionString);
                        con.Open();
                        SqlCommand cmd0 = new SqlCommand("sp_車種_刪除", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@車種代號", txt_車種_車種代號.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("刪除成功");
                        con.Close();
                    }
                    Load_車種();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void dataGrid_車種_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            
        }

        //車輛型號訊息

        private void 車輛型號訊息_新增_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_車輛型號訊息_型號.Text == null || txt_車輛型號訊息_型號.Text.ToString().Equals(""))
                    MessageBox.Show("型號沒有資料");
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    String sql = "SELECT COUNT(*) FROM 車輛型號訊息 WHERE 型號='" + txt_車輛型號訊息_型號.Text.ToString() + "'";
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() > 0)
                    {
                        MessageBox.Show("Error! '" + txt_車輛型號訊息_型號.Text.ToString() + "' 由來");
                    }
                    else
                    {
                        cmd0 = new SqlCommand("sp_車輛型號訊息_新增", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@型號", txt_車輛型號訊息_型號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@名稱", txt_車輛型號訊息_名稱.Text.ToString());
                        cmd0.Parameters.AddWithValue("@備註", txt_車輛型號訊息_備註.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("新增成功");
                        Load_車輛型號訊息();
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void 車輛型號訊息_修改_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_車輛型號訊息_型號.Text == null || txt_車輛型號訊息_型號.Text.ToString().Equals(""))
                    MessageBox.Show("型號沒有資料");
                else
                {
                    DialogResult answer = MessageBox.Show("您確定要修改資料 ?", "警報", MessageBoxButtons.YesNo);
                    if (answer == DialogResult.Yes)
                    {
                        SqlConnection con = new SqlConnection(connectionString);
                        con.Open();
                        SqlCommand cmd0 = new SqlCommand("sp_車輛型號訊息_修改", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@型號1", car);
                        cmd0.Parameters.AddWithValue("@型號2", txt_車輛型號訊息_型號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@名稱", txt_車輛型號訊息_名稱.Text.ToString());
                        cmd0.Parameters.AddWithValue("@備註", txt_車輛型號訊息_備註.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("修改成功");
                        con.Close();
                    }
                    Load_車輛型號訊息();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void 車輛型號訊息_刪除_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_車輛型號訊息_型號.Text == null || txt_車輛型號訊息_型號.Text.ToString().Equals(""))
                    MessageBox.Show("型號沒有資料");
                else
                {
                    DialogResult answer = MessageBox.Show("您確定要刪除資料 ?", "警報", MessageBoxButtons.YesNo);
                    if (answer == DialogResult.Yes)
                    {
                        SqlConnection con = new SqlConnection(connectionString);
                        con.Open();
                        SqlCommand cmd0 = new SqlCommand("sp_車輛型號訊息_刪除", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@型號", txt_車輛型號訊息_型號.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("刪除成功");
                        con.Close();
                    }
                    Load_車輛型號訊息();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void dataGrid_車輛型號訊息_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            
        }

        //車輛保養管理
        public void Load_車輛()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_車輛_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGrid_車輛.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dataGrid_車輛.DataSource = dt;
                dataGrid_車輛.EnableHeadersVisualStyles = false;
                dataGrid_車輛.ColumnHeadersDefaultCellStyle.BackColor = Color.Cyan;
                PublicClass.setRowNumber(dataGrid_車輛);
                con.Close();
                gBAdd_車輛.Visible = false;
                this.Size = new System.Drawing.Size(this.Size.Width, height - gBAdd_車輛.Height);
                this.DesktopLocation = new Point(0, 0);
                txt_車輛_牌照號碼.Text = "";
                txt_車輛號碼.Text = "";
                txt_車輛_車種代號.Text = "";
                txt_里程.Text = "";
                txt_車輛_廠牌代號.Text = "";
                txt_引擎號碼.Text = "";
                txt_車輛_型號.Text = "";
                txt_車輛_人員代號.Text = "";
                //****
                txt_車輛號碼.Enabled = true;
                txt_車輛_牌照號碼.Enabled = true;
                txt_車輛_車種代號.Enabled = true;
                txt_里程.Enabled = true;
                txt_車輛_廠牌代號.Enabled = true;
                txt_引擎號碼.Enabled = true;
                dateTime_年份.Enabled = true;
                txt_車輛_型號.Enabled = true;
                dateTime_預保日.Enabled = true;
                dateTime_進廠日.Enabled = true;
                dateTime_出廠日.Enabled = true;
                txt_車輛_人員代號.Enabled = true;
                bt_車種代號_search.Enabled = true;
                bt__廠牌代號_search.Enabled = true;
                bt_型號_search.Enabled = true;
                bt_人員代號_search.Enabled = true;
                //****
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void 車輛_新增_Click(object sender, EventArgs e)
        {
                iflag = 1;
                //btn_檢查.Visible = false;
                this.Size = new System.Drawing.Size(width, height);
                gBAdd_車輛.Visible = true;
                bt_車輛_新增_修改_刪除.Text = "新增";
        }

        private void btnEditproduct_Click(object sender, EventArgs e)
        {
                iflag = 2;
                //btn_檢查.Visible = false;
                this.Size = new System.Drawing.Size(width, height);
                gBAdd_車輛.Visible = true;
                bt_車輛_新增_修改_刪除.Text = "修改";
        }

        private void 車輛_刪除_Click(object sender, EventArgs e)
        {
                iflag = 3;
                //btn_檢查.Visible = false;
                this.Size = new System.Drawing.Size(width, height);
                gBAdd_車輛.Visible = true;
                txt_車輛號碼.Enabled = false;
                txt_車輛_牌照號碼.Enabled = false;
                txt_車輛_車種代號.Enabled = false;
                txt_里程.Enabled = false;
                txt_車輛_廠牌代號.Enabled = false;
                txt_引擎號碼.Enabled = false;
                dateTime_年份.Enabled = false;
                txt_車輛_型號.Enabled = false;
                dateTime_預保日.Enabled = false;
                dateTime_進廠日.Enabled = false;
                dateTime_出廠日.Enabled = false;
                txt_車輛_人員代號.Enabled = false;
                bt_車種代號_search.Enabled = false;
                bt__廠牌代號_search.Enabled = false;
                bt_型號_search.Enabled = false;
                bt_人員代號_search.Enabled = false;
                bt_車輛_新增_修改_刪除.Text = "刪除";
        }

        private void 車輛_查詢_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(connectionString);
                con.Open();
                string sp = "sp_車輛_查詢";
                SqlCommand cmd0 = new SqlCommand(sp, con);
                cmd0.CommandType = CommandType.StoredProcedure;
                if (txt_車輛號碼_search.Text != "") cmd0.Parameters.AddWithValue("@車輛號碼", txt_車輛號碼_search.Text.ToString());
                if (txt_車輛_牌照號碼_search.Text != "") cmd0.Parameters.AddWithValue("@牌照號碼", txt_車輛_牌照號碼_search.Text.ToString());
                if (txt_車種_search.Text != "") cmd0.Parameters.AddWithValue("@車種代號", txt_車種_search.Text.ToString());
                if (txt_里程_search.Text != "") cmd0.Parameters.AddWithValue("@里程", txt_里程_search.Text.ToString());
                if (txt_廠牌_search.Text != "") cmd0.Parameters.AddWithValue("@廠牌代號", txt_廠牌_search.Text.ToString());
                if (txt_引擎號碼_search.Text != "") cmd0.Parameters.AddWithValue("@引擎號碼", txt_引擎號碼_search.Text.ToString());
                cmd0.Parameters.AddWithValue("@年份1", DateTime.Parse(dateTime_年份_search1.Text.ToString()).ToString("yyyy-MM-dd HH:MM:ss"));
                cmd0.Parameters.AddWithValue("@年份2", DateTime.Parse(dateTime_年份_search2.Text.ToString()).ToString("yyyy-MM-dd HH:MM:ss"));
                if (txt_型號_search.Text != "") cmd0.Parameters.AddWithValue("@型號", txt_型號_search.Text.ToString());
                cmd0.Parameters.AddWithValue("@預保日1", DateTime.Parse(dateTime_車輛_預保日1.Text.ToString()).ToString("yyyy-MM-dd HH:MM:ss"));
                cmd0.Parameters.AddWithValue("@預保日2", DateTime.Parse(dateTime_車輛_預保日2.Text.ToString()).ToString("yyyy-MM-dd HH:MM:ss"));
                cmd0.Parameters.AddWithValue("@進廠日1", DateTime.Parse(dateTime_車輛_進廠日1.Text.ToString()).ToString("yyyy-MM-dd HH:MM:ss"));
                cmd0.Parameters.AddWithValue("@進廠日2", DateTime.Parse(dateTime_車輛_進廠日2.Text.ToString()).ToString("yyyy-MM-dd HH:MM:ss"));
                cmd0.Parameters.AddWithValue("@出廠日1", DateTime.Parse(dateTime_車輛_出廠日1.Text.ToString()).ToString("yyyy-MM-dd HH:MM:ss"));
                cmd0.Parameters.AddWithValue("@出廠日2", DateTime.Parse(dateTime_車輛_出廠日2.Text.ToString()).ToString("yyyy-MM-dd HH:MM:ss"));
                if (txt_車輛_成本中心.Text != "") cmd0.Parameters.AddWithValue("@車輛成本中心", txt_車輛_成本中心.Text.ToString());
                if (txt_車輛_部門代號.Text != "") cmd0.Parameters.AddWithValue("@部門代號", txt_車輛_部門代號.Text.ToString());
                if (txt_車輛_聯絡人.Text != "") cmd0.Parameters.AddWithValue("@人員名稱", txt_車輛_聯絡人.Text.ToString());
                if (txt_車輛_電話.Text != "") cmd0.Parameters.AddWithValue("@電話", txt_車輛_電話.Text.ToString());
                SqlDataReader sqlreader = cmd0.ExecuteReader();
                DataTable dt = new DataTable();
                
                 dt.Columns.Add("車輛號碼");
                 dt.Columns.Add("牌照號碼");
                 dt.Columns.Add("車種");
                 dt.Columns.Add("里程");
                 dt.Columns.Add("廠牌");
                 dt.Columns.Add("引擎號碼");
                 dt.Columns.Add("年份");
                 dt.Columns.Add("型號");
                 dt.Columns.Add("預保日");
                 dt.Columns.Add("進廠日");
                 dt.Columns.Add("出廠日");
                
                while (sqlreader.Read())
                {
                    DataRow dtrows = dt.NewRow();
                    dtrows["車輛號碼"] = sqlreader["車輛號碼"];
                    dtrows["牌照號碼"] = sqlreader["牌照號碼"];
                    dtrows["車種"] = sqlreader["車種"];
                    dtrows["里程"] = sqlreader["里程"];
                    dtrows["廠牌"] = sqlreader["廠牌"];
                    dtrows["引擎號碼"] = sqlreader["引擎號碼"];
                    dtrows["年份"] = sqlreader["年份"];
                    dtrows["型號"] = sqlreader["型號"];
                    dtrows["預保日"] = sqlreader["預保日"];
                    dtrows["進廠日"] = sqlreader["進廠日"];
                    dtrows["出廠日"] = sqlreader["出廠日"];
                    dt.Rows.Add(dtrows);
                }

                dataGrid_車輛.DataSource = dt;
                PublicClass.setRowNumber(dataGrid_車輛);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //con.Close();
            }
        }

        private void txt__車種代號_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 車種 where 車種代號=N'{0}'", txt_車輛_車種代號.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lb_車種.Text = "";
                else lb_車種.Text = temp;
                con.Close();
            }
            catch (Exception ex) { }
        }

        private void bt_人員代號_search_Click(object sender, EventArgs e)
        {
            VehicleSearch frm = new VehicleSearch(txt_車輛_人員代號);
            frm.ShowDialog();
        }

        private void 車輛_取消_search_Click(object sender, EventArgs e)
        {
            //cb_年份.Checked = false;
            txt_型號_search.Text = "";
            txt_廠牌_search.Text = "";
            txt_引擎號碼_search.Text = "";
            dateTime_年份_search1.Value = dateTime_年份_search1.MinDate;
            dateTime_年份_search2.Value = dateTime_年份_search2.MaxDate;
            dateTime_車輛_出廠日1.Value = dateTime_車輛_出廠日1.MinDate;
            dateTime_車輛_出廠日2.Value = dateTime_車輛_出廠日2.MaxDate;
            dateTime_車輛_進廠日1.Value = dateTime_車輛_進廠日1.MinDate;
            dateTime_車輛_進廠日2.Value = dateTime_車輛_進廠日2.MaxDate;
            dateTime_車輛_預保日1.Value = dateTime_車輛_預保日1.MinDate;
            dateTime_車輛_預保日2.Value = dateTime_車輛_預保日2.MaxDate;
            txt_車種_search.Text = "";
            txt_車輛_牌照號碼_search.Text = "";
            txt_車輛號碼_search.Text = "";
            txt_里程_search.Text = "";
            txt_車輛_部門代號.Text = "";
            txt_車輛_成本中心.Text = "";
            txt_車輛_聯絡人.Text = "";
            txt_車輛_電話.Text = "";
            Load_車輛();
        }

        private void bt_成本中心_search_Click(object sender, EventArgs e)
        {
            VehicleSearch frm = new VehicleSearch(txt_成本中心);
            frm.ShowDialog();
        }

        private void dataGrid_車輛_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(connectionString);
                con.Open();
                string sp = "sp_車輛_gridView";
                SqlCommand cmd0 = new SqlCommand(sp, con);
                cmd0.CommandType = CommandType.StoredProcedure;
                DataGridViewRow row = new DataGridViewRow();
                row = dataGrid_車輛.Rows[e.RowIndex];
                cmd0.Parameters.AddWithValue("@車輛號碼", row.Cells[0].Value.ToString());
                //DateTime.Parse(row.Cells[9].Value.ToString()).ToString("yyyy-MM-dd HH:MM:ss"));
                cmd0.Parameters.AddWithValue("@預保日", DateTime.Parse(row.Cells[8].Value.ToString()).ToString("yyyy-MM-dd HH:MM:ss"));
                SqlDataReader sqlreader = cmd0.ExecuteReader();
                while (sqlreader.Read())
                {
                    txt_車輛號碼.Text = sqlreader["車輛號碼"].ToString();
                    car = sqlreader["車輛號碼"].ToString();
                    txt_車輛_牌照號碼.Text = sqlreader["牌照號碼"].ToString();
                    txt_車輛_車種代號.Text = sqlreader["車種代號"].ToString();
                    txt_里程.Text = sqlreader["里程"].ToString();
                    txt_車輛_廠牌代號.Text = sqlreader["廠牌代號"].ToString();
                    txt_引擎號碼.Text = sqlreader["引擎號碼"].ToString();
                    dateTime_年份.Text = sqlreader["年份"].ToString();
                    txt_車輛_型號.Text = sqlreader["型號"].ToString();
                    dateTime_預保日.Text = sqlreader["預保日"].ToString();
                    date = (DateTime)sqlreader["預保日"];
                    dateTime_進廠日.Text = sqlreader["進廠日"].ToString();
                    dateTime_出廠日.Text = sqlreader["出廠日"].ToString();
                    txt_車輛_人員代號.Text = sqlreader["人員代號"].ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void tabControl_車輛_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl_車輛.SelectedIndex == 0)
            {
                Load_車輛();
                PublicClass.setRowNumber(dataGrid_車輛);
            }
            if (tabControl_車輛.SelectedIndex == 1)
            {
                Load_車輛人員資料();
                PublicClass.setRowNumber(dataGrid_車輛人員資料);
            }
            if (tabControl_車輛.SelectedIndex == 2)
            {
                Load_車輛成本中心();
                PublicClass.setRowNumber(dataGridCenter);
            }
            if (tabControl_車輛.SelectedIndex == 3)
            {
                Load_車輛廠牌();
                PublicClass.setRowNumber(dataGrid_車輛廠牌);
            }
            if (tabControl_車輛.SelectedIndex == 4)
            {
                Load_車種();
                PublicClass.setRowNumber(dataGrid_車種);
            }
            if (tabControl_車輛.SelectedIndex == 5)
            {
                Load_車輛型號訊息();
                PublicClass.setRowNumber(dataGrid_車輛型號訊息);
            }
        }

        private void dataGrid_車輛人員資料_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                DataGridViewRow row = new DataGridViewRow();
                row = dataGrid_車輛人員資料.Rows[e.RowIndex];
                txt_人員代號.Text = row.Cells[0].Value.ToString();
                car = row.Cells[0].Value.ToString();
                txt_人員名稱.Text = row.Cells[1].Value.ToString();
                txt_部門代號.Text = row.Cells[2].Value.ToString();
                txt_部門名稱.Text = row.Cells[3].Value.ToString();
                txt_成本中心.Text = row.Cells[4].Value.ToString();
                txt_電話.Text = row.Cells[5].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void dataGridCenter_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                DataGridViewRow row = new DataGridViewRow();
                row = dataGridCenter.Rows[e.RowIndex];
                txt_成本中心代號.Text = row.Cells[0].Value.ToString();
                car = row.Cells[0].Value.ToString();
                txt_名稱.Text = row.Cells[1].Value.ToString();
                txt_備註.Text = row.Cells[2].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void dataGrid_車輛廠牌_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                DataGridViewRow row = new DataGridViewRow();
                row = dataGrid_車輛廠牌.Rows[e.RowIndex];
                txt_廠牌代號.Text = row.Cells[0].Value.ToString();
                car = row.Cells[0].Value.ToString();
                txt_車輛廠牌_名稱.Text = row.Cells[1].Value.ToString();
                txt_車輛廠牌_備註.Text = row.Cells[2].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void dataGrid_車種_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                DataGridViewRow row = new DataGridViewRow();
                row = dataGrid_車種.Rows[e.RowIndex];
                txt_車種_車種代號.Text = row.Cells[0].Value.ToString();
                car = row.Cells[0].Value.ToString();
                txt_車種_名稱.Text = row.Cells[1].Value.ToString();
                txt_車種_備註.Text = row.Cells[2].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void dataGrid_車輛型號訊息_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                DataGridViewRow row = new DataGridViewRow();
                row = dataGrid_車輛型號訊息.Rows[e.RowIndex];
                txt_車輛型號訊息_型號.Text = row.Cells[0].Value.ToString();
                car = row.Cells[0].Value.ToString();
                txt_車輛型號訊息_名稱.Text = row.Cells[1].Value.ToString();
                txt_車輛型號訊息_備註.Text = row.Cells[2].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void txt_車輛_廠牌代號_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 車輛廠牌 where 廠牌代號=N'{0}'", txt_車輛_廠牌代號.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lb_廠牌.Text = "";
                else lb_廠牌.Text = temp;
                con.Close();
            }
            catch (Exception ex) { }
        }

        private void txt_車輛_人員代號_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 車輛人員資料 where 人員代號=N'{0}'", txt_車輛_人員代號.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lb_人員.Text = "";
                else lb_人員.Text = temp;
                con.Close();
            }
            catch (Exception ex) { }
        }

        private void txt_車輛_型號_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 車輛型號訊息 where 型號=N'{0}'", txt_車輛_型號.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lb_型號.Text = "";
                else lb_型號.Text = temp;
                con.Close();
            }
            catch (Exception ex) { }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void bt_車輛人員資料_取消_Click(object sender, EventArgs e)
        {
            txt_人員代號.Text = "";
            txt_人員名稱.Text = "";
            txt_部門代號.Text = "";
            txt_部門名稱.Text = "";
            txt_成本中心.Text = "";
            txt_電話.Text = "";
        }

        private void bt__中心_取消_Click(object sender, EventArgs e)
        {
            txt_成本中心代號.Text = "";
            txt_名稱.Text = "";
            txt_備註.Text = "";
        }

        private void bt_車輛廠牌_取消_Click(object sender, EventArgs e)
        {
            txt_車輛廠牌_備註.Text = "";
            txt_車輛廠牌_名稱.Text = "";
            txt_廠牌代號.Text = "";
        }

        private void bt_車種_取消_Click(object sender, EventArgs e)
        {
            txt_車種_車種代號.Text = "";
            txt_車種_名稱.Text = "";
            txt_車種_備註.Text = "";
        }

        private void bt_車輛型號訊息_取消_Click(object sender, EventArgs e)
        {
            txt_車輛型號訊息_備註.Text = "";
            txt_車輛型號訊息_名稱.Text = "";
            txt_車輛型號訊息_型號.Text = "";
        }

        private void gBAdd_車輛_Enter(object sender, EventArgs e)
        {

        }

        private void dateTime__預保日_ValueChanged(object sender, EventArgs e)
        {

        }

        private void bt_車輛_新增_修改_刪除_Click_1(object sender, EventArgs e)
        {
            if (iflag == 1)
            {
                if (txt_車輛號碼.Text.ToString() == null || txt_車輛號碼.Text.ToString().Equals(""))
                    MessageBox.Show("車輛號碼還沒輸入資料");
                else
                {
                    try
                    {
                        SqlConnection con = new SqlConnection(connectionString);
                        con.Open();
                        SqlCommand cmd0 = new SqlCommand("sp_車輛_新增", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@車輛號碼", txt_車輛號碼.Text.ToString());
                        cmd0.Parameters.AddWithValue("@牌照號碼", txt_車輛_牌照號碼.Text.ToString());
                        cmd0.Parameters.AddWithValue("@車種代號", txt_車輛_車種代號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@里程", txt_里程.Text.ToString());
                        cmd0.Parameters.AddWithValue("@廠牌代號", txt_車輛_廠牌代號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@引擎號碼", txt_引擎號碼.Text.ToString());
                        cmd0.Parameters.AddWithValue("@年份", DateTime.Parse(dateTime_年份.Text.ToString()).ToString("yyyy-MM-dd HH:MM:ss"));
                        cmd0.Parameters.AddWithValue("@型號", txt_車輛_型號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@預保日", dateTime_預保日.Value);
                        cmd0.Parameters.AddWithValue("@進廠日", dateTime_進廠日.Value);
                        cmd0.Parameters.AddWithValue("@出廠日", dateTime_出廠日.Value);
                        cmd0.Parameters.AddWithValue("@人員代號", txt_車輛_人員代號.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("新增成功");
                        Load_車輛();
                        con.Close();
                    }
                    catch
                    {
                        MessageBox.Show("資料不對");
                    }
                }
            }
            if (iflag == 2)
            {
                if (txt_車輛號碼.Text.ToString() == null || txt_車輛號碼.Text.ToString().Equals(""))
                    MessageBox.Show("車輛號碼還沒輸入資料");
                else
                {
                    try
                    {
                        DialogResult answer = MessageBox.Show("您確定要修改資料 ?", "警報", MessageBoxButtons.YesNo);
                        if (answer == DialogResult.Yes)
                        {
                            SqlConnection con = new SqlConnection(connectionString);
                            con.Open();
                            SqlCommand cmd0 = new SqlCommand("sp_車輛_修改", con);
                            cmd0.CommandType = CommandType.StoredProcedure;
                            cmd0.Parameters.AddWithValue("@車輛號碼_1", car);
                            cmd0.Parameters.AddWithValue("@車輛號碼_2", txt_車輛號碼.Text.ToString());
                            cmd0.Parameters.AddWithValue("@牌照號碼", txt_車輛_牌照號碼.Text.ToString());
                            cmd0.Parameters.AddWithValue("@車種代號", txt_車輛_車種代號.Text.ToString());
                            cmd0.Parameters.AddWithValue("@里程", txt_里程.Text.ToString());
                            cmd0.Parameters.AddWithValue("@廠牌代號", txt_車輛_廠牌代號.Text.ToString());
                            cmd0.Parameters.AddWithValue("@引擎號碼", txt_引擎號碼.Text.ToString());
                            cmd0.Parameters.AddWithValue("@年份", DateTime.Parse(dateTime_年份.Text.ToString()).ToString("yyyy-MM-dd HH:MM:ss"));
                            cmd0.Parameters.AddWithValue("@型號", txt_車輛_型號.Text.ToString());
                            cmd0.Parameters.AddWithValue("@預保日_1", date);
                            cmd0.Parameters.AddWithValue("@預保日_2", dateTime_預保日.Value);
                            cmd0.Parameters.AddWithValue("@進廠日", dateTime_進廠日.Value);
                            cmd0.Parameters.AddWithValue("@出廠日", dateTime_出廠日.Value);
                            cmd0.Parameters.AddWithValue("@人員代號", txt_車輛_人員代號.Text.ToString());
                            cmd0.ExecuteNonQuery();
                            MessageBox.Show("修改成功");
                            con.Close();
                        }
                        Load_車輛();
                    }
                    catch
                    {
                        MessageBox.Show("資料不對、修改不成功");
                    }
                }
            }
            if (iflag == 3)
            {
                if (txt_車輛號碼.Text.ToString() == null || txt_車輛號碼.Text.ToString().Equals(""))
                    MessageBox.Show("車輛號碼還沒輸入資料");
                else
                {
                    try
                    {
                        DialogResult answer = MessageBox.Show("您確定要刪除資料 ?", "警報", MessageBoxButtons.YesNo);
                        if (answer == DialogResult.Yes)
                        {
                            SqlConnection con = new SqlConnection(connectionString);
                            con.Open();
                            SqlCommand cmd0 = new SqlCommand("sp_車輛_刪除", con);
                            cmd0.CommandType = CommandType.StoredProcedure;
                            cmd0.Parameters.AddWithValue("@車輛號碼", txt_車輛號碼.Text.ToString());
                            cmd0.Parameters.AddWithValue("@預保日", dateTime_預保日.Value);
                            cmd0.ExecuteNonQuery();
                            MessageBox.Show("刪除成功");
                            con.Close();
                        }
                        Load_車輛();
                    }
                    catch
                    {
                        MessageBox.Show("資料不對、可以資料在使用、刪除不成功");
                    }
                }
            }
        }

        private void bt_車種代號_search_Click_1(object sender, EventArgs e)
        {
            VehicleSearch frm = new VehicleSearch(txt_車輛_車種代號);
            frm.ShowDialog();
        }

        private void bt__廠牌代號_search_Click_1(object sender, EventArgs e)
        {
            VehicleSearch frm = new VehicleSearch(txt_車輛_廠牌代號);
            frm.ShowDialog();
        }

        private void bt_型號_search_Click_1(object sender, EventArgs e)
        {
            VehicleSearch frm = new VehicleSearch(txt_車輛_型號);
            frm.ShowDialog();
        }

        private void bt_人員代號_search_Click_1(object sender, EventArgs e)
        {
            VehicleSearch frm = new VehicleSearch(txt_車輛_人員代號);
            frm.ShowDialog();
        }

        private void txt_車輛_車種代號_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 車種 where 車種代號=N'{0}'", txt_車輛_車種代號.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lb_車種.Text = "";
                else lb_車種.Text = temp;
                con.Close();
            }
            catch (Exception ex) { }
        }

        private void txt_車輛_廠牌代號_TextChanged_1(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 車輛廠牌 where 廠牌代號=N'{0}'", txt_車輛_廠牌代號.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lb_廠牌.Text = "";
                else lb_廠牌.Text = temp;
                con.Close();
            }
            catch (Exception ex) { }
        }

        private void txt_車輛_型號_TextChanged_1(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 車輛型號訊息 where 型號=N'{0}'", txt_車輛_型號.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lb_型號.Text = "";
                else lb_型號.Text = temp;
                con.Close();
            }
            catch (Exception ex) { }
        }

        private void txt_車輛_人員代號_TextChanged_1(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 車輛人員資料 where 人員代號=N'{0}'", txt_車輛_人員代號.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lb_人員.Text = "";
                else lb_人員.Text = temp;
                con.Close();
            }
            catch (Exception ex) { }
        }

        private void 車輛_取消_Click_1(object sender, EventArgs e)
        {
            width = this.Size.Width;
            height = this.Size.Height;
            gBAdd_車輛.Visible = false;
            this.Size = new System.Drawing.Size(this.Size.Width, this.Size.Height - gBAdd_車輛.Height);
            this.DesktopLocation = new Point(0, 0);
            txt_車輛_牌照號碼.Text = "";
            txt_車輛號碼.Text = "";
            txt_車輛_車種代號.Text = "";
            txt_里程.Text = "";
            txt_車輛_廠牌代號.Text = "";
            txt_引擎號碼.Text = "";
            txt_車輛_型號.Text = "";
            txt_車輛_人員代號.Text = "";
            //****
            txt_車輛號碼.Enabled = true;
            txt_車輛_牌照號碼.Enabled = true;
            txt_車輛_車種代號.Enabled = true;
            txt_里程.Enabled = true;
            txt_車輛_廠牌代號.Enabled = true;
            txt_引擎號碼.Enabled = true;
            dateTime_年份.Enabled = true;
            txt_車輛_型號.Enabled = true;
            dateTime_預保日.Enabled = true;
            dateTime_進廠日.Enabled = true;
            dateTime_出廠日.Enabled = true;
            txt_車輛_人員代號.Enabled = true;
            bt_車種代號_search.Enabled = true;
            bt__廠牌代號_search.Enabled = true;
            bt_型號_search.Enabled = true;
            bt_人員代號_search.Enabled = true;
            //****
        }
    }
}
